package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("link") //命名空间（可以不加/，他自己会帮你加）
public class LinkController {

    @Autowired
    private ILinkService service;

    @Autowired
    private ILinkService linkService;
    @RequestMapping("/findAll")
    public void findAll(HttpServletResponse resp) throws Exception {
        resp.setContentType("text/html;charset=utf-8");

        List<LinkBean> linkList = linkService.findAll();
        String str = "<table border='1' width = '60%'>";
        str+="<thead><tr>" +
                "<th>姓名</th><th>生日</th><th>电话</th><th>操作</th>" +
                "</tr></thead>";
        str+="<tbody>";
        for (LinkBean linkBean : linkList){
            str+="<tr><td>"+linkBean.getName()+"</td><td>"+linkBean.getBirthday()+"</td><td>"+linkBean.getPhone()
                    +"</td><td><a href='/link/del?id="+linkBean.getId()+"'>删除</a>" +
                    "<a href ='/link/findById?id="+linkBean.getId()+"'>修改</a></td></tr>";
        }
        str+="</tbody></table>";


        str += "<a href = '/html/addLink.html'>添加</a>";
        resp.getWriter().print(str); 
    }

    @RequestMapping("add")
    public String add(LinkBean link){

        service.add(link);

        return "redirect:/link/cutFindAll";
    }

    @RequestMapping("/del/{linkId}")
    public String del(@PathVariable("linkId") int id){
        service.del(id);
        return "redirect:/link/cutFindAll";
    }

    @RequestMapping("findById")
    public void findById(int id,HttpServletResponse response) throws Exception{

        response.setContentType("text/html;charset=utf-8");

        LinkBean linkBean = service.findById(id);

        String str = "<form action='/link/update'>";
        str += "<input type = 'hidden' name='id' value='" + id + "'>";
        str += "姓名：" + linkBean.getName() + "<br>";
        str += "生日：" + linkBean.getBirthday() + "<br>";
        str += "生日:<input type='text' name='phone' value='" + linkBean.getPhone() + "'><br>";
        str += "<input type='submit' value='修改'>";

        str += "</form>";
        response.getWriter().print(str);
    }

    @PostMapping("update") //PostMapping 表示这个方法只能用post方式请求
    public String update(int id,String phone){
        service.update(id,phone);

        return "redirect:/link/cutFindAll";
    }

    //method = RequestMethod.GET  表示这个方法只能用get方式进行访问，否则会报405请求方式错误
    @RequestMapping(value = "cutFindAll",method = RequestMethod.GET)
    public void cutFindAll(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, HttpServletResponse resp) throws Exception {

        resp.setContentType("text/html;charset=utf-8");

//        //(1)传入页码
//        String str = req.getParameter("pageNO");
//        int pageNO = str == null ? 1 : Integer.parseInt(str);

        PageInfo<LinkBean> pageObj=service.cutFindAll(pageNo);

        String infoStr = "<table border='1' width='60%'>";
        infoStr+="<thead><tr><th>姓名</th><th>生日</th><th>电话</th><th>操作</th></tr></thead>";
        infoStr+="<tbody>";


        for (LinkBean linkBean : pageObj.getList()) {
            infoStr+="<tr>" +
                    "<td>"+linkBean.getName()+"</td>" +
                    "<td>"+linkBean.getBirthday()+"</td>" +
                    "<td>"+linkBean.getPhone()+"</td>" +
                    "<td><a href='/link/del/"+linkBean.getId()+"'>删除</a>" +
                    " <a href='/link/findById?id="+linkBean.getId()+"'>修改</a>" +
                    "</td>" +
                    "</tr>";
        }

        infoStr+="</tbody>";
        infoStr+="</table>";

        //页码
        for (int i = 1; i <= pageObj.getPages(); i++) {
            infoStr += "<a href='/link/cutFindAll?pageNo=" + i + "'>" + i + "</a> ";
        }
        infoStr+="<a href='/html/addLink.html'>添加</a>";

        resp.getWriter().print(infoStr);

    }
}
